package com.u.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.u.character.Manager;
import com.u.character.Processer;
import com.u.character.Repairer;
import com.u.character.Role;
import com.u.character.Station;

import net.sf.json.JSONArray;

/**
 * Servlet implementation class GetRole
 */
@WebServlet("/GetRole")
public class GetRole extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetRole() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		String method = request.getParameter("method");
		HttpSession httpSession = request.getSession();
		Role role = (Role) httpSession.getAttribute("Role");
		JSONArray json = new JSONArray();
		if(role == null) {
			json.add("none");
		}else {
			role.setPassword("*****");
			switch(role.getUsertype()) {
			case("manager"):role = new Manager(role);break;
			case("processer"):role = new Processer(role);break;
			case("station"):role = new Station(role);break;
			case("repairer"):role = new Repairer(role);break;
			default:break;
			}
			try {
				switch(method) {
				case("ne"):{
					if((role.getUsertype()).equals("station")) {
						json.add("ok");
					}else {
						json.add("no");
					}
				}
				case("mo"):{
					if((role.getUsertype()).equals("station")) {
						json.add("ok");
					}else if((role.getUsertype()).equals("processer")) {
						json.add("ok");
					}
				}
				case("lo"):{
					json.add("ok");
				}
				}
			}catch(NullPointerException e) {
				json.add("have");
			}
		}
		PrintWriter out = response.getWriter();
		out.println(json.toString());
		out.close();
	}
	

}
